package com.zshan.clinic.web.controller.clinic;


import com.zshan.clinic.admin.entity.dto.ClinicEmpDTO;
import com.zshan.clinic.admin.entity.dto.ResetPasswordDTO;
import com.zshan.clinic.admin.entity.vo.ClinicEmpInfoVo;
import com.zshan.clinic.admin.entity.vo.ClinicEmpSelectVo;
import com.zshan.clinic.admin.entity.vo.ClinicEmpSignatureVo;
import com.zshan.clinic.admin.entity.vo.ClinicEmpVo;
import com.zshan.clinic.admin.service.clinic.ClinicEmpService;
import com.zshan.clinic.common.enums.PasswordChangeSourceEnum;
import com.zshan.clinic.common.enums.YesOrNoEnum;
import com.zshan.clinic.common.response.RestResp;
import com.zshan.clinic.common.util.page.PageInfo;
import com.zshan.clinic.database.model.ClinicEmp;
import com.zshan.clinic.database.model.ClinicEmpSignature;
import com.zshan.clinic.web.controller.AbstractController;
import com.zshan.clinic.web.entity.UserInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 * 员工管理
 */
@RestController
@RequestMapping("/clinic/emp")
public class ClinicEmpController extends AbstractController {

    @Autowired
    private ClinicEmpService clinicEmpService;

    /**
     * 个人中心：获取个人签名
     * @param
     * @return
     */
    @GetMapping("/getEmpSignature")
    public RestResp<ClinicEmpSignatureVo> getEmpSignature(){
        UserInfo userInfo = getUserInfo();
        ClinicEmpSignatureVo clinicEmpSignatureVo = clinicEmpService.getEmpSignature(userInfo.getClinicId(),userInfo.getEmpId());
        return RestResp.SUCCESS(clinicEmpSignatureVo);
    }

    /**
     * 个人中心：修改个人签名
     * @return
     */
    @PostMapping(value = "/updateSignature")
    public RestResp<Void> updateSignature(@RequestBody ClinicEmpSignature clinicEmpSignature) {
        UserInfo userInfo = getUserInfo();
        clinicEmpSignature.setEmpId(userInfo.getEmpId());
        clinicEmpSignature.setCreateUserId(userInfo.getEmpId());
        clinicEmpSignature.setCreateBy(userInfo.getEmpName());
        clinicEmpSignature.setUpdateUserId(userInfo.getEmpId());
        clinicEmpSignature.setUpdateBy(userInfo.getEmpName());
        clinicEmpSignature.setClinicId(userInfo.getClinicId());
        clinicEmpService.updateSignature(clinicEmpSignature);
        return RestResp.SUCCESS();
    }

    /**
     * 个人中心：修改密码
     * @return
     */
    @PostMapping(value = "/updatePassword")
    public RestResp<Void> updatePassword(@RequestBody ResetPasswordDTO resetPasswordDTO) {
        UserInfo userInfo = getUserInfo();
        resetPasswordDTO.setEmpId(userInfo.getEmpId());
        resetPasswordDTO.setUpdateUserId(userInfo.getEmpId());
        resetPasswordDTO.setUpdateBy(userInfo.getEmpName());
        resetPasswordDTO.setClinicId(userInfo.getClinicId());
        resetPasswordDTO.setSource(PasswordChangeSourceEnum.PERSONAL_CENTER.getValue());
        clinicEmpService.resetPassword(resetPasswordDTO);
        return RestResp.SUCCESS();
    }

    /**
     * 工作台/个人中心：查询员工个人信息
     * @param
     * @return
     */
    @GetMapping("/getEmpInfo")
    public RestResp<ClinicEmpInfoVo> getEmpInfo(){
        UserInfo userInfo = getUserInfo();
        ClinicEmpInfoVo clinicEmpInfoVo = clinicEmpService.getEmpInfo(userInfo.getClinicId(),userInfo.getEmpId());
        return RestResp.SUCCESS(clinicEmpInfoVo);
    }

    /**
     * 个人中心：员工编辑简介
     * @return
     */
    @PostMapping(value = "/updateNotes")
    public RestResp<Void> updateNotes(@RequestBody ClinicEmp clinicEmp) {
        UserInfo userInfo = getUserInfo();
        clinicEmp.setEmpId(userInfo.getEmpId());
        clinicEmp.setUpdateUserId(userInfo.getEmpId());
        clinicEmp.setUpdateBy(userInfo.getEmpName());
        clinicEmp.setClinicId(userInfo.getClinicId());
        clinicEmpService.updateNotes(clinicEmp);
        return RestResp.SUCCESS();
    }

    /**
     * 查询诊所员工下拉框数据
     * @return
     */
    @GetMapping("/getSelectList")
    public RestResp<List<ClinicEmpSelectVo>> getSelectList(){
        UserInfo userInfo = getUserInfo();
        List<ClinicEmpSelectVo> selectList = clinicEmpService.getSelectList(userInfo.getClinicId());
        return RestResp.SUCCESS(selectList);
    }


    /**
     * 查询员工列表
     * @param
     * @return
     */
    @GetMapping("/getEmpList")
    public RestResp<PageInfo<ClinicEmpVo>> getEmpList(@RequestParam(name = "empName",required = false) String empName,
                                                      @RequestParam(name = "deptId",required = false) Long deptId,
                                                      @RequestParam(name = "roleId",required = false) Long roleId,
                                                      @RequestParam(name = "pageNo", defaultValue = "1", required = false) Integer pageNo,
                                                      @RequestParam(name = "pageSize", defaultValue = "10", required = false) Integer pageSize){
        UserInfo userInfo = getUserInfo();
        PageInfo<ClinicEmpVo> pageInfo = clinicEmpService.getClinicEmpList(userInfo.getClinicId(),empName,deptId,roleId,pageNo,pageSize);
        return RestResp.SUCCESS(pageInfo);
    }

    /**
     * 查询员工详情
     * @param empId
     * @return
     */
    @GetMapping("/getEmpDetail")
    public RestResp<ClinicEmpVo> getEmpDetail(@RequestParam(name = "empId") Long empId){
        UserInfo userInfo = getUserInfo();
        ClinicEmpVo clinicEmpVo = clinicEmpService.getEmpDetail(userInfo.getClinicId(),empId);
        return RestResp.SUCCESS(clinicEmpVo);
    }

    /**
     * 添加员工
     * @return
     */
    @PostMapping(value = "/add")
    public RestResp<Void> add(@RequestBody ClinicEmpDTO clinicEmpDTO) {
        UserInfo userInfo = getUserInfo();
        clinicEmpDTO.setCreateUserId(userInfo.getEmpId());
        clinicEmpDTO.setCreateBy(userInfo.getEmpName());
        clinicEmpDTO.setClinicId(userInfo.getClinicId());
        clinicEmpService.add(clinicEmpDTO);
        return RestResp.SUCCESS();
    }

    /**
     * 编辑员工
     * @return
     */
    @PostMapping(value = "/update")
    public RestResp<Void> update(@RequestBody ClinicEmpDTO clinicEmpDTO) {
        UserInfo userInfo = getUserInfo();
        clinicEmpDTO.setUpdateUserId(userInfo.getEmpId());
        clinicEmpDTO.setUpdateBy(userInfo.getEmpName());
        clinicEmpDTO.setClinicId(userInfo.getClinicId());
        clinicEmpService.update(clinicEmpDTO);
        return RestResp.SUCCESS();
    }

    /**
     * 启用员工
     * @return
     */
    @PostMapping(value = "/enable")
    public RestResp<Void> enable(@RequestBody ClinicEmpDTO clinicEmpDTO) {
        UserInfo userInfo = getUserInfo();
        clinicEmpDTO.setUpdateUserId(userInfo.getEmpId());
        clinicEmpDTO.setUpdateBy(userInfo.getEmpName());
        clinicEmpDTO.setClinicId(userInfo.getClinicId());
        clinicEmpService.enable(clinicEmpDTO);
        return RestResp.SUCCESS();
    }

    /**
     * 停用员工
     * @return
     */
    @PostMapping(value = "/disable")
    public RestResp<Void> disable(@RequestBody ClinicEmpDTO clinicEmpDTO) {
        UserInfo userInfo = getUserInfo();
        clinicEmpDTO.setUpdateUserId(userInfo.getEmpId());
        clinicEmpDTO.setUpdateBy(userInfo.getEmpName());
        clinicEmpDTO.setClinicId(userInfo.getClinicId());
        clinicEmpService.disable(clinicEmpDTO);
        return RestResp.SUCCESS();
    }

    /**
     * 角色管理：重置员工密码
     * @return
     */
    @PostMapping(value = "/resetPassword")
    public RestResp<Void> resetPassword(@RequestBody ResetPasswordDTO resetPasswordDTO) {
        UserInfo userInfo = getUserInfo();
        resetPasswordDTO.setUpdateUserId(userInfo.getEmpId());
        resetPasswordDTO.setUpdateBy(userInfo.getEmpName());
        resetPasswordDTO.setClinicId(userInfo.getClinicId());
        resetPasswordDTO.setSource(PasswordChangeSourceEnum.ROLE_MANAGEMENT.getValue());
        clinicEmpService.resetPassword(resetPasswordDTO);
        return RestResp.SUCCESS();
    }
}
